package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.common.RedisConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.User;
import com.itheima.health.service.UserService;
import com.itheima.health.util.ValidateCodeUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.InputStream;
import java.util.concurrent.TimeUnit;

/**
 * 验证码Controller
 */

/*任务8 手机验证码登录 肖丁豪*/
@RestController
@RequestMapping("/mobile/validateCode")
@Slf4j
@Api(tags = "手机端验证码接口")
public class MobileValidateCodeController {
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 任务8 根据用户手机号 给用户发送验证码
     * 任务10-任务8合并 手机端提交预约-发送验证码
     * @param type 验证码类型，用于区分不同的业务
     * @param telephone 手机号
     * @return
     */
    @PostMapping("/send")
    public Result send(String type,String telephone){
        //1.获取验证码
        InputStream resourceAsStream =
                this.getClass().getClassLoader().getResourceAsStream("");

        Integer code = ValidateCodeUtils.generateValidateCode(4);
        log.info("我们要发送给用户的验证码是:"+code);
        //2.发送短信
        System.out.println("发送的短信为：" + code);
        //3.储存到redis中
        String key = RedisConst.VALIDATE_CODE_PREFIX +"_"+type+"_"+telephone;
        redisTemplate.opsForValue().set(key,code,2,TimeUnit.MINUTES);
        //4.返回验证码发送成功
        return new Result(true,MessageConst.SEND_VALIDATECODE_SUCCESS);
    }
}
